﻿Imports System.Data.OleDb

Partial Public Class EditProfile
    Inherits System.Web.UI.Page



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_UpdateProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_UpdateProfile.Click
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim deleteOldInfoSql As String = "DELETE * FROM UserInformation WHERE UserName = @UserName"
        Dim DeleteOldInfoCmd As New OleDbCommand(deleteOldInfoSql, conn)
        DeleteOldInfoCmd.Parameters.AddWithValue("@UserName", User.Identity.Name)
        conn.Open()
        DeleteOldInfoCmd.ExecuteNonQuery()

        Dim sql As String = "INSERT INTO UserInformation(FirstName,LastName,Age,Gender,City,Country,AboutMe,Interests,ProfileImage,UserName,EmailAddress,Birthdate,UserId) VALUES(@FirstName,@LastName,@Age,@Gender,@City,@Country,@AboutMe,@Interests,@ProfileImage,@UserName,@EmailAddress,@Birthdate,@UserId)"

        Dim fileLocationOnServer = Request.MapPath("Images/ProfileImages/" & FileUpload_ProfileImage.FileName)
        Dim relativePath As String = "Images/ProfileImages/" & FileUpload_ProfileImage.FileName
        Try
            FileUpload_ProfileImage.SaveAs(fileLocationOnServer)
        Catch ex As Exception
            'FileUpload_ProfileImage.SaveAs(Request.MapPath("Images/ProfileImages/defaultProfileImage.jpg"))
        End Try


        Dim dbcmd = New OleDbCommand(sql, conn)
        dbcmd.Parameters.AddWithValue("@FirstName", tb_FirstName.Text)
        dbcmd.Parameters.AddWithValue("@LastName", tb_LastName.Text)
        dbcmd.Parameters.AddWithValue("@Age", getUserAge())
        dbcmd.Parameters.AddWithValue("@Gender", RadioButtonList_Gender.SelectedValue.ToString())
        dbcmd.Parameters.AddWithValue("@City", tb_City.Text)
        dbcmd.Parameters.AddWithValue("@Country", tb_Country.Text)
        dbcmd.Parameters.AddWithValue("@AboutMe", tb_AboutMe.Text)
        dbcmd.Parameters.AddWithValue("@Interests", tb_Interests.Text)
        dbcmd.Parameters.AddWithValue("@ProfileImage", relativePath)
        dbcmd.Parameters.AddWithValue("@UserName", User.Identity.Name)
        dbcmd.Parameters.AddWithValue("EmailAddress", tb_Email.Text)
        dbcmd.Parameters.AddWithValue("@Birthdate", tb_Birthdate.Text)
        dbcmd.Parameters.AddWithValue("@UserId", getUserId())
        dbcmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("memberprofile.aspx?UserName=" & User.Identity.Name)
    End Sub

    Function getUserAge() As Integer
        Dim birthdate As Date
        birthdate = tb_Birthdate.Text
        Dim UserAge As Integer = DateTime.Now.Year - birthdate.Year
        If birthdate.Month <= DateTime.Now.Month Then
            If birthdate.Month = DateTime.Now.Month AndAlso birthdate.Day > DateTime.Now.Day Then
                UserAge -= 1
            End If
        Else
            If birthdate.Month > DateTime.Now.Month Then
                UserAge -= 1
            End If
        End If
        Return UserAge
    End Function

    Function getUserID() As Integer
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim loginName = User.Identity.Name
        Dim loginId As Integer
        Dim readLoginIdSQL As String = "SELECT UserId FROM aspnet_Users where UserName = @UserName"
        Dim cmd As New OleDbCommand(readLoginIdSQL, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@UserName", loginName)
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        reader.Read()
        loginId = (reader("UserId"))
        conn.Close()
        Return loginId

    End Function
End Class